package com.example.bijiabao.controller;


import com.example.bijiabao.entity.user;
import com.example.bijiabao.services.userServices;
import com.example.bijiabao.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class userController {
    @Autowired
    private userServices userServices;
//用户登录
    @GetMapping("/check")
    public Result checkuser(@RequestParam("userName") String userName, @RequestParam("password") String password){
        Result result=new Result();
        user user=userServices.checkuser(userName);
        if(user!=null){
                if(user.getPassword().equals(password)){
                    result.setCode(202);
                    result.setMessage("登录成功！");
                }
                else{
                    result.setCode(203);
                    result.setMessage("密码错误！");
                }
            }
        else {
            result.setCode(200);
            result.setMessage("该用户不存在！");
        }
        return result;
    }
    //    用户注册
    @PostMapping ("/register")
    public Result registerUser(@RequestBody user user){
        Result result=new Result();
        String userName=user.getUserName();
        user user1=userServices.checkuser(userName);
        if(user1==null){
            userServices.registerUser(user);
            result.setCode(200);
            result.setMessage("注册成功！");
        }else{
            result.setCode(300);
            result.setMessage("该用户已存在！请直接登录！");
        }
        return result;
    }

}